<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/_init_model.php';
	
	class mod_package extends _init_model
	{
		public $PAK_ID;
		public $PAK_NAME;
		public $PAK_PRICE;
		public $PAK_C_ID;
		public $PAK_LEVEL;
		
		function __construct(){
			parent::__construct();
				
			$this->tableName = "package";
		}
		
		function get_package_list() {
			$sqlcmd = 'SELECT PAK_ID, PAK_NAME';
			$sqlcmd .= ' FROM ' . $this->tableName;
			$sqlcmd .= ' WHERE PAK_ID NOT IN (1)';			
			$query = $this->db->query($sqlcmd);
					
			return $query->result_array();
		}
		function get_package($package_id, $fields=null, $load_config=false){
			if (!is_null($fields))
				$this->db->select($fields);
			
			if ($load_config)
				$this->db->join('package_config', 'package_config.PAK_ID = package.PAK_ID');			
			
			$this->db->where('package.PAK_ID', $package_id);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row_array(0);
			else
				return null;
		}
		function get_package_level($id){
			$this->db->select('PAK_LEVEL');		
			$this->db->where('PAK_ID', $id);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row(0)->PAK_LEVEL;
			else
				return null;
		}
		function get_free_package_detail(){
			return $this->get_package(1, null, true);
		}
		function calculate_price($package_id, $day_amount){
			$package = $this->get_package($package_id, 'PAK_PRICE');
			return $package['PAK_PRICE'] * $day_amount;			
		}	
		
		function compare_package($order_package_id, $current_package_id){
			$order_package_lv = $this->get_package_level($order_package_id);
			$current_package_lv = $this->get_package_level($current_package_id);
			
			if ($order_package_lv == $current_package_lv)
				return 0; // same package
			elseif ($order_package_lv > $current_package_lv)
				return 1; // above package
			elseif ($order_package_lv < $current_package_lv)
				return -1; // lower package
		}	
	}
?>